import json

class Message:
    def __init__(self, msg):
        msg = json.loads(msg.decode('utf-8'))
        self.type = msg['type']
        if msg['type'] <= 3 or msg['type'] == 5:
            self.data = msg['data']
        if msg['type'] == 3:
            self.fr = msg['from']
        if msg['type'] == 4 or msg['type'] == 6:
            self.user = msg['user']
            self.pwd = msg['pwd']

    @staticmethod
    def len(size):
        return json.dumps({'type':1, 'data':size})

    @staticmethod
    def notice(msg):
        return json.dumps({'type':2, 'data':msg})

    @staticmethod
    def broadcast(fr, msg):
        return json.dumps({'type':3, 'from': fr, 'data':msg})

    @staticmethod
    def login(user, pwd):
        return json.dumps({'type':4, 'user': user, 'pwd':pwd})

    @staticmethod
    def login_result(res):
        return json.dumps({'type':5, 'data':res})

    @staticmethod
    def register(user, pwd):
        return json.dumps({'type':6, 'user': user, 'pwd':pwd})